package net.sourceforge.peertwitter.core.applications.communication.messaging;

import rice.p2p.commonapi.Message;

public class ReliableMessageEnvelope implements ReliableMessage {

	/**
	 * Generated serial UID
	 */
	private static final long serialVersionUID = -56502610028793090L;
	private static final int DEFAULT_PRIORITY = Message.MEDIUM_HIGH_PRIORITY;
	private Message message;
	private int priority;

	public ReliableMessageEnvelope(Message message) {
		this.message = message;
		this.priority = getGreaterPriority(DEFAULT_PRIORITY, message.getPriority());
	}

	private int getGreaterPriority(int p1, int p2) {
		return Math.min(p1, p2); //the lesser the number, the greater the priority
	}

	@Override
	public int getPriority() {
		return this.priority;
	}

	@Override
	public Message getMessage() {
		return this.message;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((message == null) ? 0 : message.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ReliableMessageEnvelope other = (ReliableMessageEnvelope) obj;
		if (message == null) {
			if (other.message != null)
				return false;
		} else if (!message.equals(other.message))
			return false;
		return true;
	}

}
